import 'dart:ui' as ui;

import 'package:flutter/material.dart';

///
/// 绘制语音输入底部圆弧
///
class OnnxMaskOvalPainter extends CustomPainter {
  bool isFocus;

  OnnxMaskOvalPainter({
    this.isFocus = true,
  });

  @override
  void paint(Canvas canvas, Size size) {
    /// 绘制椭圆形
    final bgOvalRect = Rect.fromCenter(
      center: Offset(size.width * 0.5, size.height * 1.5),
      width: size.width * 1.8,
      height: size.height * 3,
    );

    final paint = Paint()
      ..color = const Color(0xff393939)
      ..style = PaintingStyle.fill;

    Path path = Path()..addOval(bgOvalRect);

    if (isFocus) {
      paint.color = const Color(0xffb0b0b0);
      canvas.drawPath(path, paint);

      final scale = (size.height * 3 - 8) / (size.height * 3);

      final bgShaderRect = Rect.fromCenter(
        center: bgOvalRect.center,
        width: bgOvalRect.width * scale,
        height: bgOvalRect.height * scale,
      );
      canvas.drawPath(
        Path()..addOval(bgShaderRect),
        Paint()
          ..shader = ui.Gradient.linear(
            Offset(size.width / 2, size.height),
            Offset(size.width / 2, 0),
            [
              const Color(0xffd5d5d5),
              const Color(0xff999999),
            ],
          ),
      );
    } else {
      canvas.drawPath(path, paint);
    }
  }

  @override
  bool shouldRepaint(covariant OnnxMaskOvalPainter oldDelegate) => oldDelegate.isFocus != isFocus;

  @override
  bool shouldRebuildSemantics(covariant CustomPainter oldDelegate) => false;

  @override
  bool hitTest(Offset position) => false; // 不拦截触摸事件，提升性能
}
